import java.math.BigInteger;


public class MultiplyStrings {
	public void tester() {
		long a = (int)(Math.random()*10000);
		long b = (int)(Math.random()*10000);
		if(! (""+a*b).equals(multiply(""+a,""+b)))
			System.out.println(a+" "+b);
		
//		System.out.println(multiply("1234","0"));
	}
	
	public String multiply(String num1, String num2) {
	    int length1 = num1.length();
	    int length2 = num2.length();
	    int[] result = new int[length1 + length2];
	    for (int i = length1 - 1; i >= 0; i--) {
	        int tem1 = num1.charAt(i) - '0';
	        for (int j = length2 - 1; j >= 0; j--) {
	            int tem2 = num2.charAt(j) - '0';
	            int temMultiply = tem1 * tem2;
	            int carry = temMultiply / 10;
	            int temResult = temMultiply % 10;
	            result[i + j + 1] += temResult;
	            if (result[i + j + 1] >= 10) {
	                result[i + j + 1] = result[i + j + 1] % 10;
	                carry++;
	            }
	            result[i + j] += carry;
	            if (result[i + j] >= 10) {
	                result[i + j] = result[i + j] % 10;
	                result[i + j - 1] += 1;
	            }
	        }

	    }
	    StringBuffer sb = new StringBuffer();
	    boolean start = false;
	    for (int k = 0; k <= length1 + length2 - 1; k++) {
	        if (result[k] != 0) {
	            start = true;
	        }
	        if (start) {
	            sb.append(result[k]);
	        }
	    }
	    if (!start)
	        return 0 + "";
	    return sb.toString();
	}
	
	public String multiply2(String num1, String num2) {
		if(num2.length() > num1.length()) {
			String t = num2;
			num2 = num1;
			num1 = t;
		}
		
        String result = "0";
        String zero = "";
        for(int i = num2.length() - 1; i >= 0 ; i--) {
        	String temp = multiply(num1, num2.charAt(i));
        	temp = temp + zero;
        	
        	result = add(result, temp);
        	zero = zero + "0";
        }
        
        while(result.length() > 1 && result.charAt(0) == '0') {
    		result = result.substring(1);
    	}
		
		return result;
    }
    
    public String add(String num1, String num2) {
        String result = "";
        int curr = 0, next = 0;
        
        int t1 = 0, t2 = 0;
        
        for(int i = 0; i < num1.length() || i < num2.length() ; i++) {
            if(i >= num1.length()) t1 = 0;
            else t1 = num1.charAt(num1.length() - i - 1)-'0';
            
            if(i >= num2.length()) t2 = 0;
            else t2 = num2.charAt(num2.length() - i - 1)-'0';
            
            curr = next + t1 + t2;
            next = curr/10;
            curr = curr%10;
            result = curr + result;
        }
        
        if(next == 0) return result;
        
        result = next + result;
        
        return result;
    }
    
    public String multiply(String num1, char c2) {
        String result = "";
        int curr = 0, next = 0;
        for(int i = num1.length()-1 ; i>= 0 ; i--) {
            curr = next + (num1.charAt(i)-'0')*(c2-'0');
            next = curr/10;
            curr = curr % 10;
            result = curr + result;
        }
        if(next == 0) return result;
        result = next + result;
        return result;
    }
}
